package com.yt.lxh.search.service.impl;

import cn.hutool.core.util.StrUtil;
import com.rabbitmq.client.Channel;
import com.yt.lxh.constant.MQConst;
import com.yt.lxh.search.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
public class listenSkuNotify {

    @Autowired
    private SkuService skuService;

    // 监听商品上架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConst.GOODS_UPPER_QUEUE, durable = "true"),
            exchange = @Exchange(name = MQConst.GOODS_DIRECT_EXCHANGE),
            key = {MQConst.GOODS_UPPER_KEY}
    ))
    public void receiveGoodsUpper(String skuId, Message message, Channel channel) throws IOException {
        if (StrUtil.isNotBlank(skuId)) skuService.upperSku(skuId);
        // 手动确认收到消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    // 监听商品下架
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MQConst.GOODS_LOWER_QUEUE, durable = "true"),
            exchange = @Exchange(value = MQConst.GOODS_DIRECT_EXCHANGE),
            key = {MQConst.GOODS_LOWER_KEY}
    ))
    public void receiveGoodsLower(String skuId, Message message, Channel channel) throws IOException {
        if (StrUtil.isNotBlank(skuId)) skuService.lowerSku(skuId);
        // 手动确认收到消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}
